{createAction} = require('redux-actions')
Toast = require('antd-mobile/lib/toast')
{fetchLogOut} = require('./userInfo.js')
tFetch = require('../utilities/tFetch.js')
actions =
  getListStart:createAction('YHYQR/GET_LIST_START')
  getListCompleted:createAction('YHYQR/GET_LIST_COMPLETED')
  refreshListStart:createAction('YHYQR/REFRESH_LIST_START')
  refreshListCompleted:createAction('YHYQR/REFRESH_LIST_COMPLETED')
  refreshListStart:createAction('YHYQR/REFRESH_LIST')
  pilotConfirmfail:createAction('YHYQR/PILOT_CONFIRM_FAIL')
  pilotConfirmCompleted:createAction('YHYQR/PILOT_CONFIRM_COMPLETED')
  pilotCancelFail:createAction('YHYQR/PILOT_CANCEL_FAIL')
  pilotCancelCompleted:createAction('YHYQR/PILOT_CANCEL_COMPLTED')
  fetchPilotConfirm:(planid,planno,token)->
    debugger
    (dispatch)->
      fetch("http://202.136.214.173/api/v_short_pilotcallback",{
          method: 'POST'
          mode: 'cors'
          cache: 'default'
          headers: {
            'Content-Type':'application/x-www-form-urlencoded'
            token:token
          }
          body:"planid=#{planid}&planno=#{planno}&state=1"
        })
      .then (res)->
        debugger
        if res.ok
          res.json()

      .then (json)->
        dispatch actions.pilotConfirmCompleted
          planid:planid
        Toast.success("确认成功")
      .catch (err)->
        dispatch actions.pilotConfirmfail(err)
        Toast.fail('确认失败，请重试')
  fetchPilotCancel:(planid,planno,token)->
    (dispatch)->
      fetch("http://202.136.214.173/api/v_short_pilotcallback",{
          method: 'POST'
          mode: 'cors'
          cache: 'default'
          headers: {
            'Content-Type':'application/x-www-form-urlencoded'
            token:token
          }
          body:"planid=#{planid}&planno=#{planno}&state=2"
        })
      .then (res)->
        if res.ok
          res.json()
      .then (json)->
        dispatch actions.pilotCancelCompleted
          planid:planid
        Toast.success("取消成功")
      .catch (err)->
        dispatch actions.pilotCancelFail(err)
        Toast.fail("取消失败，请重试")
  fetchGetList:(pageNum,token)->
    (dispatch)->
      debugger
      return
      dispatch actions.getListStart()
      fetch("http://202.136.214.173/api/v_short_pilotcallback",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers: {
            'Content-Type':'application/x-www-form-urlencoded'
            token:token
          }
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        res.json()
      .then (json)->
        #获取当前时间
        dispatch actions.getListCompleted
          list:json
          lastFetchTime:new Date()
      .catch (err)->
        console.log(err)
  fetchRefreshList:(token)->
    (dispatch)->
      dispatch actions.refreshListStart
      promise = fetch("http://202.136.214.173/api/v_short_pilotcallback?pageNum=1",{
                  method: 'GET'
                  mode: 'cors'
                  cache: 'default'
                  headers: {
                    'Content-Type':'application/x-www-form-urlencoded'
                    token:token
                  }
                  # body:"pageNum=#{pageNum}&state=1"
                })
      tFetch dispatch,promise,(json)->
        dispatch actions.refreshListCompleted
            list:json
            lastFetchTime:new Date()
module.exports = actions
